/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI.bll;

import BLL.Student;
import BLL.homepage;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author YELIUHUISHI
 */
public class Update extends javax.swing.JFrame {

    private int id;

    /**
     * Creates new form Add
     * @param id
     */
    public Update(int id) {
        this.id=id;
        initComponents();
        init();
        paddingDate();
    }

    private void paddingDate() {
        homepage homepage = new homepage();
        ResultSet resultSet = homepage.singleLog(id);
        try {
            while(resultSet.next()){
                originalId.setText(String.valueOf(resultSet.getInt("id")));
                originalName.setText(resultSet.getString("name"));
                originalSex.setText(resultSet.getString("sex"));
                originalAge.setText(String.valueOf(resultSet.getInt("age")));
                changeId.setText(String.valueOf(resultSet.getInt("id")));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void init() {
        this.setTitle("修改界面");
        this.setResizable(false);
        uiutlLogin uiutlLogin = new uiutlLogin();
        uiutlLogin.setFrameConter(this);
        uiutlLogin.setFrameTmage(this,"src"+ File.separator+ "UI" +File.separator+"icon"+File.separator+"Update.png");
        new Thread(new runnableTime(this.Time)).start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        originalId = new javax.swing.JLabel();
        originalSex = new javax.swing.JLabel();
        originalName = new javax.swing.JLabel();
        originalAge = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        changeId = new javax.swing.JLabel();
        changeName = new javax.swing.JTextField();
        changeAge = new javax.swing.JTextField();
        changeSex = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        Time = new javax.swing.JLabel();
        updateConfirm = new javax.swing.JButton();
        updateCancel = new javax.swing.JButton();


        Time.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        Time.setForeground(new java.awt.Color(255, 20, 18));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel5.setText("name");

        jLabel6.setText("id");

        jLabel7.setText("sex");

        jLabel8.setText("age");

        changeSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "男", "女" }));

        changeName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeNameActionPerformed(evt);
            }
        });

        jLabel9.setText("原本数据");

        jLabel10.setText("更换数据");

        changeAge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateConfirmActionPerformed(evt);
            }
        });

        updateConfirm.setText("确定");
        updateConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateConfirmActionPerformed(evt);
            }
        });
        updateCancel.setText("取消");
        updateCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateCancelActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(Time, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel10)))
                .addGap(30, 30, 30))
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, 0)
                        .addComponent(updateConfirm)
                        .addGap(121, 121, 121)
                        .addComponent(updateCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(originalSex, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(changeSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(originalAge, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(changeAge, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(originalId, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(changeId))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(originalName, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(changeName, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(originalId, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(changeId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(originalName, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(changeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(originalSex, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(changeSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(originalAge, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(changeAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(updateConfirm)
                    .addComponent(updateCancel))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void changeNameActionPerformed(java.awt.event.ActionEvent evt){
        changeAge.requestFocus();
    }


    //确定键的动作事件
    private void updateConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homepageFineActionPerformed
        //获取添加的内容
        String userUpdateName=changeName.getText().trim();
        String userUpdateSex=(String) changeSex.getSelectedItem();
        String userUpdateAge=changeAge.getText().trim();

        //规范
        String userNameRegex="^([\\u4e00-\\u9fa5]{1,20}|[a-zA-Z\\.\\s]{1,20})$";//验证姓名正则
        String userAgeRegex="^[0-9]+([0-9]+){0,1}$";

        if(!userUpdateName.matches(userNameRegex)){
            JOptionPane.showMessageDialog(this,"姓名不符合规范");
            changeName.setBackground(new Color(255, 22, 32));
            this.changeName.requestFocus();
            return;
        }
        if(!userUpdateAge.matches(userAgeRegex)){
            JOptionPane.showMessageDialog(this,"年龄不符");
            changeName.setBackground(new Color(209, 231, 255));
            changeAge.setBackground(new Color(255, 22, 32));
            this.changeAge.requestFocus();
            return;
        }
        homepage homepage = new homepage();
        Student student = new Student(userUpdateName, userUpdateSex, Integer.valueOf(userUpdateAge));
        boolean update = homepage.update(id, student);
        if(!update){
            JOptionPane.showMessageDialog(this, "更新成功！");
        }else{
            JOptionPane.showMessageDialog(this, "更新失败！");
            return;
        }
        new Homepage().setVisible(true);
        this.dispose();
    }
    //取消键的动作事件
    private void updateCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homepageFineActionPerformed
        new Homepage().setVisible(true);
        this.dispose();
    }


    public javax.swing.JTextField changeAge;
    public javax.swing.JLabel changeId;
    public javax.swing.JTextField changeName;
    public javax.swing.JComboBox changeSex;
    public javax.swing.JLabel jLabel10;
    public javax.swing.JLabel jLabel5;
    public javax.swing.JLabel jLabel6;
    public javax.swing.JLabel jLabel7;
    public javax.swing.JLabel jLabel8;
    public javax.swing.JLabel jLabel9;
    public javax.swing.JLabel originalAge;
    public javax.swing.JLabel originalId;
    public javax.swing.JLabel originalName;
    public javax.swing.JLabel originalSex;
    public javax.swing.JButton updateCancel;
    public javax.swing.JButton updateConfirm;
    public javax.swing.JLabel Time;
    // End of variables declaration//GEN-END:variables

}
